package qgs.zisu.javaconcurrency.javaconcurrency.chapter7_synchronized;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/11 14:36
 * @Version 1.0
 */
public class SynchronizedStatic {
    static {
        // 静态代码块 加的锁其实是 class锁，锁的是SynchronizedStatic类。
        synchronized (SynchronizedStatic.class){
            System.out.println("static :"+Thread.currentThread().getName());
            try {
                Thread.sleep(10_000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // 在静态方法上 加锁，这个锁：monitor是Class(SynchronizedStatic.class)
    public synchronized static void m1(){
        System.out.println("m1"+Thread.currentThread().getName());
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized static void m2(){
        System.out.println("m2 "+Thread.currentThread().getName());
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void m3(){
        System.out.println("m3 "+Thread.currentThread().getName());
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
